package com.example.demo6.controller;

import com.example.demo6.entity.User;
import com.example.demo6.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller

public class UserController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    UserService userService;
    @RequestMapping("/")
    public String login(){
        return "login";
    }
    @RequestMapping("/login")
    public String loginProcess(@RequestParam String username,@RequestParam String userpwd,Model model){
        String name = request.getParameter("username");
        String password = request.getParameter("password");
        HttpSession session = request.getSession();
        session.setAttribute("username", name);

        System.out.println(username);
        System.out.println(userpwd);

        User user = new User();
        user.setName(username);
        user.setPassword(userpwd);
        User userdb = userService.findOne(user);
//        model.addAttribute("data",userdb);
        System.out.println(userdb);
        if(!userdb.getName().isEmpty()){
            if (userdb.getPassword().equals(userpwd))
            return "redirect:/main";
        else {
            return "redirect:/error";
        }
    }else {
            return "redirect:/error";
        }}

    @RequestMapping("/main")
    public String main(Model model){
//        List<Goods> goodsList=goodsService.findAll();
//        model.addAttribute("data",goodsList);
//        首先判断session里面是否有用户，若有则可登录，没有不可登录，后续不可使用。
        Object username=request.getSession().getAttribute("username");
        System.out.println(username);
        if (username==null){
            return "redirect:/";
        }else{
            model.addAttribute("username",username);
            return "main1";
        }

    }
    @RequestMapping("/logout")
    public String logout(){
        request.getSession().removeAttribute("username");
        return "login";}
    @RequestMapping("/register")
    public String reg() {
        return "register";
    }
    @RequestMapping("/registerProcess")
    public String register(@RequestParam String username,@RequestParam String userpwd) {
//        接收到前端发送过来的数据
        System.out.println("接收到了数据----" + username);
        if (username.isEmpty()) {
            return "redirect:/register";

        } else {
            User user = new User();
            user.setName(username);
            user.setPassword(userpwd);
            user.setRights("0");
            user.setStatus("0");
            System.out.println(user);
            if (userService.add(user) == 1) {
                return "redirect:/";
            } else {
                return "redirect:/register";
            }
        }

    }
}